import Vue from 'vue'
import Router from 'vue-router'

import home from '@/components/home'
import showPerson from '@/components/person/person_list'
import AddPerson from '@/components/person/person_add'

import AddLogger from '@/components/logger/logger_add'
import showLogger from '@/components/logger/logger_list'

import showJianli from '@/components/jianli/jianli_list'
import AddJianli from '@/components/jianli/jianli_add'

import showPics from '@/components/picture/pic_list'
import AddPics from '@/components/picture/pic_add'

import showMusic from '@/components/music/music_list'
import AddMusic from '@/components/music/music_add'


import showArticle from '@/components/article/article_list'
import AddArticle from '@/components/article/article_add'


import admin from '@/components/admin'

Vue.use(Router)

const router=new Router({
  routes: [
    {
      path: '/',
      redirect: '/admin'
    },
    {
      path: '/admin',
      component: admin
    },
    {
      path: '/home',
      // 需要登录才能进入的页面可以增加一个meta属性
      // meta: {
      //   requireAuth: true
      // },
      component: home,
      children:[
        {
          path: '/',
          redirect: '/showPerson'
        },
        {
          path: '/showPerson',
          // 需要登录才能进入的页面可以增加一个meta属性
          meta: {
            requireAuth: true
          },
          component: showPerson
        },
        {
          path: '/addperson',
          // 需要登录才能进入的页面可以增加一个meta属性
          meta: {
            requireAuth: true
          },
          component: AddPerson
        },

        //日志管理路径配置
        {
          path: '/showLogger',
          // 需要登录才能进入的页面可以增加一个meta属性
          meta: {
            requireAuth: true
          },
          component: showLogger
        },

        {
          path: '/AddLogger',
          component: AddLogger
        },

        //简历的配置：
        {
          path: '/showJianli',
          meta: {
            requireAuth: true
          },
          component: showJianli
        },
        {
          path: '/AddJianli',
          component: AddJianli
        },


        //背景音乐管理
        {
          path: '/showMusic',
          meta: {
            requireAuth: true
          },
          component: showMusic
        },
        {
          path: '/AddMusic',
          component: AddMusic
        },


        //图片添加url
        {
          path: '/AddPics',
          component: AddPics
        },

        {
          path: '/showPics',
          meta: {
            requireAuth: true
          },
          component: showPics
        },

        //文章的配置：
        {
          path: '/showArticle',
          meta: {
            requireAuth: true
          },
          component: showArticle
        },
        {
          path: '/AddArticle',
          component: AddArticle
        },

      ]

    },
  ]
})
 // 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {// 判断是否需要登录权限
    console.log(".............",sessionStorage.getItem('user'));
    if (sessionStorage.getItem('user')){// 判断是否登录
      next()
    } else {// 没登录则跳转到登录界面
      console.log("............else");
      console.log(".............",localStorage.getItem('user'));
      next({
        path: '/admin',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})
export default router
